package com.foreverwin.jzymes.qc.point.controller;

import com.foreverwin.jzymes.common.service.ISiteAdviceService;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.annotation.ParamBind;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.base.BaseController;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.qc.point.service.IDoPointCheckService;
import com.foreverwin.jzymes.qc.point.service.IFindUndoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/qc/pointCheck")
public class PointCheckController extends BaseController {
    @Autowired
    IFindUndoneService findUndoneService;
    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    IDoPointCheckService doPointCheckService;

    @RequestMapping("/findUndone")
    public AjaxResult findUndone( @RequestParam String type ,@RequestParam String group){
         String site = CommonMethods.getSite();
         siteAdviceService.existSite( site );
        try{
            List<?> Undonelist = new ArrayList<>();
           if ( type.equals("SAMPLE") ){
               Undonelist =  findUndoneService.findSampleUndone( type , site , new Date());
           }else if ( type.equals("TOOL") ){
               Undonelist = findUndoneService.findToolUndone( type , site ,group, new Date());
           }

            return AjaxResult.returnSuccess().put("value",Undonelist);
        }catch ( BusinessException e ) {
            return AjaxResult.returnError(e.getMessage());
        }
    }

    @RequestMapping("/doSampleCheck")
    public AjaxResult doSampleCheck( @RequestBody List<Map<String,Object>> jsonMap ){
        try{
            Assert.isTrue( !jsonMap.isEmpty(), I18nUtil.getI18nText("qc.point.checkisNULL"));

            doPointCheckService.doSamplePointCheck( jsonMap );
            return AjaxResult.returnSuccess();
        }catch ( BusinessException e ) {
            return AjaxResult.returnError(e.getMessage());
        }
    }

    @RequestMapping("/doToolCheck")
    public AjaxResult doToolCheck( @RequestBody List<Map<String,Object>> jsonMap ){
        try{
            Assert.isTrue( !jsonMap.isEmpty(), I18nUtil.getI18nText("qc.point.checkisNULL"));

            doPointCheckService.doToolPointCheck( jsonMap );
            return AjaxResult.returnSuccess();
        }catch ( BusinessException e ) {
            return AjaxResult.returnError(e.getMessage());
        }
    }

}
